"""
实现模型评估
"""

from torch.optim import Adam
import torch.nn.functional as F
from tqdm import tqdm

import config
from chatbot.seq2seq import Seq2Seq
from chatbot.dataset import train_data_loader
import torch

from lib.cut_sentence import cut

import numpy as np


# 训练流程
# 1.实例化model,optimizer,loss


def eval(by_word=True):
    seq2Seq = Seq2Seq()
    seq2seq = seq2Seq.to(config.device)
    seq2seq.load_state_dict(torch.load(r"E:\shenhaitaoPyCode\chat_service\model\chatbot\seq2seq.model"))
    while True:
        _input = input("请输入...")
        _input = cut(_input, by_word=by_word)
        input_lenght = torch.LongTensor(
            [len(_input)] if len(_input) > config.chatbot_input_max_len else len(_input)).to(
            config.device)
        _input = torch.LongTensor([config.chatbot_ws_input.transform(_input, max_len=config.chatbot_input_max_len)]).to(
            config.device)
        indices = np.array(seq2seq.evaluate(_input, input_lenght)).flatten()
        output = config.chatbot_ws_target.inverse_transform(indices)

        print("answer:", output)
